package com.sanshi.project.assets.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.admin.api.dto.CodeRecordDTO;
import com.sanshi.project.admin.api.entity.CodeRecordEntity;
import com.sanshi.project.assets.service.CodeRecordService;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import com.sanshi.project.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 编码记录
 *
 * @author nikon
 * @date 2023-11-12 17:29:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/codeRecord")
@Tag(description = "codeRecord", name = "编码记录管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CodeRecordController {

    private final CodeRecordService codeRecordService;

    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param codeRecord 编码记录
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('assets_codeRecord_view')")
    public R getCodeRecordPage(@ParameterObject Page page, @ParameterObject CodeRecordDTO codeRecord) {
        LambdaQueryWrapper<CodeRecordEntity> wrapper = Wrappers.lambdaQuery();
        // 业务类型
        wrapper.in(ArrayUtil.isNotEmpty(codeRecord.getType()), CodeRecordEntity::getType, codeRecord.getType());
        // 编码规则名称
        wrapper.like(StrUtil.isNotBlank(codeRecord.getName()), CodeRecordEntity::getName, codeRecord.getName());
        // 编码值
        wrapper.like(StrUtil.isNotBlank(codeRecord.getValue()), CodeRecordEntity::getValue, codeRecord.getValue());
        // 倒序查询
        wrapper.orderByDesc(CodeRecordEntity::getUpdateTime);
        return R.ok(codeRecordService.page(page, wrapper));
    }


    /**
     * 通过id查询编码记录
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('assets_codeRecord_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(codeRecordService.getById(id));
    }

    /**
     * 新增编码记录
     *
     * @param codeRecord 编码记录
     * @return R
     */
    @Operation(summary = "新增编码记录", description = "新增编码记录")
    @SysLog("新增编码记录")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('assets_codeRecord_add')")
    public R save(@RequestBody CodeRecordEntity codeRecord) {
        return R.ok(codeRecordService.save(codeRecord));
    }

    /**
     * 修改编码记录
     *
     * @param codeRecord 编码记录
     * @return R
     */
    @Operation(summary = "修改编码记录", description = "修改编码记录")
    @SysLog("修改编码记录")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('assets_codeRecord_edit')")
    public R updateById(@RequestBody CodeRecordEntity codeRecord) {
        return R.ok(codeRecordService.updateById(codeRecord));
    }

    /**
     * 通过id删除编码记录
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除编码记录", description = "通过id删除编码记录")
    @SysLog("通过id删除编码记录")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('assets_codeRecord_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(codeRecordService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param codeRecord 查询条件
     * @param ids        导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('assets_codeRecord_export')")
    public List<CodeRecordEntity> export(CodeRecordEntity codeRecord, Long[] ids) {
        return codeRecordService.list(Wrappers.lambdaQuery(codeRecord).in(ArrayUtil.isNotEmpty(ids), CodeRecordEntity::getId, ids));
    }
}